﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RaytheonTimeWindows.Properties;
using RaytheonTimeDataModel;

namespace RaytheonTimeWindows
{
    public partial class frmSummary : Form
    {
        #region private data members

        private Workday workday_;
        private int workdayId_ = 0;

        #endregion

        #region public data members

        public Common.DataChangedDelegate DataChangedCallback;

        #endregion

        #region constructors

        public frmSummary(Int32 workdayId)
        {
            InitializeComponent();
            workdayId_ = workdayId;

            try
            {
                using (var db = new RaytheonTimeDataEntities())
                {
                    workday_ = (from w in db.Workdays
                                where w.workdayId == workdayId
                                select w).FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                String message = "Error occurred converting calling Workday Constructor\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region overriden event handlers

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            try
            {
                LoadData();
            }
            catch (Exception ex)
            {
                String message = "Error occurred converting calling Loading frmSummary\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region private event handlers

        private void frmSummary_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                String message = "Error occurred Pressing the Escape Key\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                RefreshForm();
            }
            catch (Exception ex)
            {
                String message = "Error occurred clicking on Refresh\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region private methods

        private void LoadData()
        {
            using (var db = new RaytheonTimeDataEntities())
            {
                workday_ = (from w in db.Workdays
                            where w.workdayId == workdayId_
                            select w).FirstOrDefault();

                txtWorkdayId.Text = workday_.workdayId.ToString();
                txtEmployeeId.Text = workday_.Employee.employeeNumber.Trim();
                dtpDate.Value = workday_.workdayDate;
            }
            LoadWorktime();
        }

        private void LoadWorktime()
        {
            using (var db = new RaytheonTimeDataEntities())
            {
                var worktimes = (from w in db.Worktimes
                                 where w.workdayId == workday_.workdayId
                                 group w by new
                                 {
                                     w.ChargeNumber.network,
                                     w.ChargeNumber.activity,
                                     w.ChargeNumberAttribute.attributeText,
                                     w.isFridayForwarded,
                                     w.notes
                                 } into grp
                                 orderby grp.Key.isFridayForwarded
                                 select new
                                 {
                                     network = grp.Key.network,
                                     activity = grp.Key.activity,
                                     attAbtText = grp.Key.attributeText,
                                     duration = grp.Sum(w => w.duration),
                                     isFridayForwarded = grp.Key.isFridayForwarded,
                                     notes = grp.Key.notes
                                 });

                double duration = 0.0;
                foreach (var worktimeSummary in worktimes)
                {
                    int rowIndex = dgvWorktimes.Rows.Add();

                    //network
                    dgvWorktimes.Rows[rowIndex].Cells[0].Value = worktimeSummary.network.Trim();
                    //activity
                    dgvWorktimes.Rows[rowIndex].Cells[1].Value = worktimeSummary.activity.Trim();
                    // Attendance / Absence Type Text
                    dgvWorktimes.Rows[rowIndex].Cells[2].Value = worktimeSummary.attAbtText.Trim();
                    //duration
                    dgvWorktimes.Rows[rowIndex].Cells[3].Value = worktimeSummary.duration.ToString("#.00");
                    duration += worktimeSummary.duration;
                    // isFirdayForwarded
                    dgvWorktimes.Rows[rowIndex].Cells[4].Value = worktimeSummary.isFridayForwarded;
                    // notes
                    dgvWorktimes.Rows[rowIndex].Cells[5].Value = worktimeSummary.notes.Trim();
                }

                txtHours.Text = duration.ToString("#.00");

                dgvWorktimes.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
            }
        }

        private void RefreshForm()
        {
            try
            {
                dgvWorktimes.Rows.Clear();
                LoadWorktime();
            }
            catch (Exception ex)
            {
                String message = "Error occurred Refreshing Datagrid\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateDataCallback()
        {
            try
            {
                // update current form
                RefreshForm();
                // update parent form
                DataChangedCallback();
            }
            catch (Exception ex)
            {
                String message = "Error occurred Updating Data Callback (refresh)\n";
                message += ex.Message;
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
